package com.pmsystem.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.pmsystem.pojo.Fix;
import com.pmsystem.pojo.User;
import com.pmsystem.service.FixService;
import com.pmsystem.util.BeanUtil;
import com.pmsystem.util.TableResponse;

@Controller
public class FixController {

	@Autowired
	private FixService fixService;
	
	/**
	 * 
	 * <p>Title: proposeFix</p >  
	 * <p>Description: 业主申请维修</p >  
	 * @author 泽强
	 * @param session
	 * @param fix
	 * @return
	 */
	@RequestMapping(value="user/proposeFix" , method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> proposeFix(HttpSession session,Fix fix) {
		Map<String,Object> map = new HashMap<String, Object>();
		User user = (User) session.getAttribute("user");
		fix.setUserId(user.getUserId());
		fix.setFixBillSendTime(new Date());
		fix.setIsCheck((byte)0);
		fix.setIsApply((byte)0);
		fix.setIsFinish((byte)0);
		fix.setEnable((byte)1);
		boolean flag=fixService.addFix(fix);
		String msg = null;
		if(flag) {
			 msg = "success";
		}else {
			msg= "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: updateFix</p >  
	 * <p>Description: 修改维修申请  ( 包活撤销 )</p >  
	 * @author 泽强
	 * @param fix
	 * @return
	 */
	@RequestMapping(value= {"admin/updateFix","user/updateFix"} , method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updateFix(Fix fix) {
		boolean flag=fixService.update(fix);
		Map<String,Object> map =new HashMap<>();
		String msg = null;
		if(flag) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: findFixTable</p >  
	 * <p>Description: </p >  
	 * @author 泽强
	 * @param map
	 * @return
	 */
	@RequestMapping(value="admin/findFixTable" , method=RequestMethod.GET)
	@ResponseBody
	public Object findFixTable(@RequestParam Map<String,Object> map) {
		Map<String,Object> map1 = BeanUtil.mapToMapWithoutNull(map);
		List<Map<String,Object>> list =fixService.adminFindFix(map1);
		long count = fixService.fixCount(map1);
		return new TableResponse(0,"",count,list);
	}
	
	/**
	 * 
	 * <p>Title: findFix</p >  
	 * <p>Description: </p >  
	 * @author 泽强
	 * @param map
	 * @return
	 */
	@RequestMapping(value="admin/findFix" , method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> findFix(@RequestParam Map<String,Object> map) {
		List<Map<String,Object>> list =fixService.adminFindFix(BeanUtil.mapToMapWithoutNull(map));
		return list.get(0);
	}
	
	/**
	 * 
	 * <p>Title: findFixTable</p >  
	 * <p>Description:用户  按一定条件查找维修单，注意SQL语句</p >  
	 * @author 泽强
	 * @param map
	 * @return
	 */
	@RequestMapping(value="user/findFixTable" , method=RequestMethod.GET)
	@ResponseBody
	public Object findFixTable(HttpSession session,@RequestParam Map<String,Object> map) {
		User user = (User) session.getAttribute("user");
		map.put("userId", user.getUserId());
		map.put("columnName", "fix_bill_send_time");	
		map.put("ascOrDesc", "DESC");
		Map<String,Object> map1 = BeanUtil.mapToMapWithoutNull(map);
		List<Fix> list=fixService.findFix(map1);
		long count = fixService.fixCount(map1);
		return new TableResponse(0,"",count,list);
	}
	

}
